#include"../../source/client/rpcClient.hpp"

void onConnectionCb(BaseConnection::ptr conn)
{
    LOG_DEBUG << "触发连接成功回调";
}

void onRpcResponse(BaseConnection::ptr conn, RpcResponse::ptr msg, BaseProtocol::ptr protocol)
{
    LOG_DEBUG << "触发onMessage回调:";
        {
            LOG_DEBUG << "id = " << msg->getId() << " mtype = " << (int)msg->getType();
            LOG_DEBUG << "rcode = " << (int)msg->getRcode();
            Json::Value ss = msg->getResult(); 
            LOG_INFO << "result = "  << ss[KEY_RESULT].asInt();
        }
}

void Handler(const Json::Value result)
{
    LOG_INFO << "回调结果 = " << result["sum"].asInt();
}

int main()
{
    Init_logger("INFO", "/srv/log/jsonRpc/", "net-test-client", 1024 * 1024);

    RpcClient::ptr client = std::make_shared<RpcClient>(Address("127.0.0.1", 8081));
    client->connectedRegister();

    /*
    Requestor::ptr requestor = std::make_shared<Requestor>();
    

    // 消息类型 -- 分类调度
    auto dispatcher = DispatcherFactory::create();
    dispatcher->setCallBack<RpcResponse>(MSG_Type::RPC_RES, std::bind(&Requestor::onResponse, requestor.get(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

    // 客户端（往上）
    auto client = NetFactory::creater<MuduoClient>("127.0.0.1", 8080, "client-test");
    client->setConnCb(onConnectionCb);
    client->setMessageHander(std::bind(&Dispatcher::onMessage, dispatcher.get(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

    // 应用层的数据处理设置
    BaseProtocol::ptr protocol = ProtocolFactory::create<LvProtocol>();
    client->setHandlerProtocol(protocol);
    client->start();

    // 用户接口
    RpcCaller::ptr caller = std::make_shared<RpcCaller>(requestor);
    */

    while (1)
    {
        int num1, num2;
        std::cin >> num1 >> num2;

        Json::Value parameters;
        parameters["num1"] = num1;
        parameters["num2"] = num2;

        // 同步
        Json::Value result;
        bool ret = client->callSync("Add", parameters, result);

        LOG_INFO << "同步结果 = " << result["sum"].asInt();

        // 异步
        RpcCallFuture fu = client->callAsync("Add", parameters);
        if (!fu.empty())
        {
            const Json::Value result = fu.get();
            LOG_INFO << "异步结果 = " << result["sum"].asInt();
        }

        // 回调
        client->callBack("Add", parameters, Handler);
    }

    /*
    auto Lvproto = ProtocolFactory::create<LvProtocol>();
    while (1)
    {
        auto request = MessageFactory::create<RPCRequest>();
            request->setId();
            request->setType(MSG_Type::RPC_REQ);
            request->setMethod("Add");
            Json::Value val;
            int a, b;
            std::cin >> a >> b;
            val["left"] = a;
            val["right"] = b;
            request->setParameters(val);

            std::string out = Lvproto->serialize(request);
            if (!out.empty())
            {
                auto conn = client->getClientConnection();
                if (conn && conn->connected())
                {
                    conn->send(out);
                }
                else
                {
                    LOG_DEBUG << "连接已被断开, 对端主机：" << conn->perrAddress();
                    return -1;
                }
            }
    }
            */
    return 0;
}